package com.obas.integration.element.telnet.loader;

import java.util.ArrayList;
import java.util.List;

import org.springframework.util.StringUtils;

import com.ease.common.util.FileUtil;
import com.ease.common.util.OutputUtil;
import com.ease.common.vo.editor.AbstractObjectEditor;
import com.obas.integration.element.telnet.editor.ZTES8902TelnetEditor;
import com.obas.model.business.DeviceMacAddress;

public class ZTES8902TelnetDeviceMacAddressLoader extends ZTEDefaultTelnetDeviceMacAddressLoader {

	public AbstractObjectEditor<DeviceMacAddress> getRawEditor() {
		return new ZTES8902TelnetEditor();
	}

	public List<DeviceMacAddress> editTextAsObject(String rawDeviceMacAddress) {
		AbstractObjectEditor<DeviceMacAddress> rawEditor = this.getRawEditor();

		String[] rows = StringUtils.delimitedListToStringArray(rawDeviceMacAddress, "\r\n");
		if (notEmpty(rows)) {
			List<DeviceMacAddress> deviceMacAddresses = new ArrayList<DeviceMacAddress>();
			boolean start = false;
			for (String row : rows) {

				if (!notEmpty(row)) {
					continue;
				}

				if (row.startsWith("  MAC_Address    vid  vpn        port  per stc toS wtd srF dsF Frm     Time")) {
					start = true;
					continue;
				}
				if (row.startsWith("-------------------------")) {
					continue;
				}
				if (row.endsWith("#") || row.endsWith(">")) {
					break;
				}
				if (true == start) {
					rawEditor.setAsText(row);
					DeviceMacAddress deviceMacAddress = rawEditor.getObject();
					if (null != deviceMacAddress) {
						deviceMacAddresses.add(deviceMacAddress);
					}
				}
			}
			return escapeEmpty(deviceMacAddresses);
		}
		return null;
	}

	public static void main(String[] args) {

		ZTES8902TelnetDeviceMacAddressLoader retriever = new ZTES8902TelnetDeviceMacAddressLoader();

		retriever.setHost("192.168.239.130");
		retriever.setPort(23);
		retriever.setUsername("zxr10");
		retriever.setPassword("zxr10");

		retriever.setUsernamePrompt("Username:");
		retriever.setPasswordPrompt("Password:");
		retriever.setMorePrompt("---- More ----");
		retriever.setCommand("show mac");

		List<DeviceMacAddress> deviceMacAddresses = retriever.loadDeviceMacAddress();

		OutputUtil.p(deviceMacAddresses);
	}

	public static void main1(String[] args) {
		String rawDeviceMacAddress = FileUtil.readClassPathResourceToString(ZTES8902TelnetDeviceMacAddressLoader.class, "ZTE S8902.cmd.txt");
		List<DeviceMacAddress> deviceMacAddresses = new ZTES8902TelnetDeviceMacAddressLoader().editTextAsObject(rawDeviceMacAddress);
		OutputUtil.p(deviceMacAddresses);
	}
}
